home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / egebook.lha / ege.book / 3 / multiple.C < prev    next >
C/C++ Source or Header  |  1992-06-05  |  2KB  |  94 lines

  1. #include <stdio.h>
  2. #include "bool.h"
  3.  
  4. // Class definition for Person
  5.  
  6. class Person {
  7.   char* name;
  8. public:
  9.   Person();                // constructor
  10.   Person(char *n);         //    bodies declared elsewhere
  11.   void print();            // member function
  12. };
  13.  
  14. // Body for class Person
  15.  
  16.  Person::Person(){         // constructor
  17.    name = "";
  18.  }
  19.  
  20.  Person::Person(char *n){  // constructor
  21.    name = n;
  22.  };
  23.  
  24.  void Person::print(){     // member functions
  25.    printf("my name is: %s \n", name);
  26.  };
  27.  
  28. // Class definition for Teacher
  29.                                 // Teacher is derived 
  30. class Teacher: public Person {  //        from Person
  31.   int courses;
  32.   static int MaxCourses;
  33. public:
  34.   Teacher(char *name):Person(name){
  35.     courses = 0;
  36.   }
  37.   bool check();
  38.   void addCourse();
  39. };
  40.  
  41. // Body for class Teacher
  42.  
  43. int Teacher::MaxCourses = 2;
  44.  
  45. bool Teacher::check(){
  46.   return (bool) (courses < MaxCourses);
  47. }
  48.  
  49. void Teacher::addCourse() {
  50.   if (check()) 
  51.     courses++; 
  52. }
  53.  
  54. class Researcher {
  55.   char *expertise;          // field of expertise
  56. public:
  57.   Researcher(char *e){      // constructor
  58.     expertise = e;
  59.   }
  60.   void print(){
  61.     printf("I am an expert in: %s\n", expertise);
  62.   }
  63. };
  64.  
  65. class Professor: public Teacher, public Researcher {
  66.   enum nature {teacher, researcher} kind;
  67. public:
  68.                           // combined constructor
  69.   Professor(char *name, char* exp):
  70.       Teacher(name),Researcher(exp){};
  71.  
  72.   void change(){          // change nature
  73.     if (kind == teacher)
  74.        kind = researcher;
  75.     else
  76.        kind = teacher;
  77.   }
  78.   void print(){           // print according to nature
  79.     Teacher::print();
  80.     if (kind == researcher) {
  81.        printf("and ");
  82.        Researcher::print();
  83.     }
  84.   }
  85. };
  86.  
  87. main(){
  88.   Professor p("Albert Einstein","Relativity");
  89.  
  90.   p.print();     // professor defaults to a teacher
  91.   p.change();    // change to researcher
  92.   p.print();    
  93. }
  94.